import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class Ex32 {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("请输入进程数量: ");
            int n = scanner.nextInt();
            List<Process> ProcessList = new ArrayList<>();
            // 输入进程信息
            for (int i = 0; i < n; i++) {
                System.out.print("请输入进程ID, 到达时间, 服务时间（以空格分隔）: ");
                double id = scanner.nextDouble();
                double arrivalTime = scanner.nextDouble();
                double burstTime = scanner.nextDouble();
                Process process = new Process(id, arrivalTime, burstTime);
                ProcessList.add(process);
            }
            Procressing procressing = new Procressing();
            procressing.print(procressing.completingInformation(ProcessList));
        }
    }
}

class Process {
    double id; // 进程ID
    double arrivalTime; // 到达时间
    double burstTime; // 服务时间
    double completionTime; // 完成时间
    double turnaroundTime; // 周转时间

    public Process(double id, double arrivalTime, double burstTime) {
        this.id = id;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
    }

    public void setCompletionTime(double completionTime) {
        this.completionTime = completionTime;
    }

    public void setTurnaroundTime(double turnaroundTime) {
        this.turnaroundTime = turnaroundTime;
    }
}

class Procressing {
    Process process;
    double totalTurnaroundTime;// 周转时间
    double totalWeightedTurnaroundTime;// 带权周转时间
    double averageTurnaroundTime;
    double averageWeightedTurnaroundTime;

    public List<Process> completingInformation(List<Process> ProcessList) {
        for (int i = 0; i < ProcessList.size(); i++) {
            process = ProcessList.get(i);
            if (i == 0) {
                process.setCompletionTime(process.arrivalTime + process.burstTime);
            } else {
                process.setCompletionTime(ProcessList.get(i - 1).completionTime + process.burstTime);
            }
            totalTurnaroundTime += process.completionTime - process.arrivalTime;

        }
        for (int i = 0; i < ProcessList.size(); i++) {
            process = ProcessList.get(i);
            process.setTurnaroundTime(process.completionTime - process.arrivalTime);
            totalWeightedTurnaroundTime += (process.turnaroundTime / process.burstTime);
        }
        averageTurnaroundTime = totalTurnaroundTime / ProcessList.size();
        averageWeightedTurnaroundTime = totalWeightedTurnaroundTime / ProcessList.size();
        return ProcessList;
    }

    public void print(List<Process> ProcessList) {
        for (int i = 0; i < ProcessList.size(); i++) {
            process = ProcessList.get(i);
            System.out.println(
                    "进程ID:" + process.id + ", 完成时间:" + process.completionTime + ", 周转时间: " + process.turnaroundTime);
        }
        System.out.println("平均周转时间为：" + averageTurnaroundTime + ", 平均带权周转时间为：" + averageWeightedTurnaroundTime);
    }
}
